package org.jmc.refmanager.domain.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity()
@Table(name = "v_adresse")
public class Adresse implements Serializable {
	private static final long serialVersionUID = 1L;

	@EmbeddedId
	private AdressePK pk;
	
	@Column(name="commune", length=255)
	private String commune;

	@Column(name="codeDepartement", length=2)
	private String codeDepartement;

	@Column(name="codeRegion", length=3)
	private String codeRegion;

	@Column(name="codeDialogDepartement", length=2)
	private String codeDialogDepartement;

	@Column(name="codeTGDepartement", length=3)
	private String codeTGDepartement;

	
	/**
	 * @return the codePostal
	 */
	public String getCodePostal() {
		if (pk == null) {
			return null;
		} else {
			return pk.getCodePostal();
		}
	}

	/**
	 * @param codePostal the codePostal to set
	 */
	public void setCodePostal(String codePostal) {
		if (pk == null) {
			this.pk = new AdressePK();
		}
		this.pk.setCodePostal(codePostal);
	}

	/**
	 * @return the insee
	 */
	public String getInsee() {
		if (pk == null) {
			return null;
		} else {
			return pk.getInsee();
		}
	}

	/**
	 * @param insee the insee to set
	 */
	public void setInsee(String insee) {
		if (pk == null) {
			this.pk = new AdressePK();
		}
		this.pk.setInsee(insee);
	}

	/**
	 * 
	 */
	public Adresse() {
		super();
	}


	/**
	 * 
	 */
	public Adresse(String codePostal, String insee) {
		this(new AdressePK(codePostal, insee));
	}

	/**
	 * 
	 */
	public Adresse(AdressePK pk) {
		super();
		this.pk = pk;
	}

	/**
	 * @return the commune
	 */
	public String getCommune() {
		return commune;
	}

	/**
	 * @param commune the commune to set
	 */
	public void setCommune(String commune) {
		this.commune = commune;
	}

	/**
	 * @return the codeDepartement
	 */
	public String getCodeDepartement() {
		return codeDepartement;
	}

	/**
	 * @param codeDepartement the codeDepartement to set
	 */
	public void setCodeDepartement(String codeDepartement) {
		this.codeDepartement = codeDepartement;
	}

	/**
	 * @return the codeRegion
	 */
	public String getCodeRegion() {
		return codeRegion;
	}

	/**
	 * @param codeRegion the codeRegion to set
	 */
	public void setCodeRegion(String codeRegion) {
		this.codeRegion = codeRegion;
	}

	/**
	 * @return the codeDialogDepartement
	 */
	public String getCodeDialogDepartement() {
		return codeDialogDepartement;
	}

	/**
	 * @param codeDialogDepartement the codeDialogDepartement to set
	 */
	public void setCodeDialogDepartement(String codeDialogDepartement) {
		this.codeDialogDepartement = codeDialogDepartement;
	}

	/**
	 * @return the codeTGDepartement
	 */
	public String getCodeTGDepartement() {
		return codeTGDepartement;
	}

	/**
	 * @param codeTGDepartement the codeTGDepartement to set
	 */
	public void setCodeTGDepartement(String codeTGDepartement) {
		this.codeTGDepartement = codeTGDepartement;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return String
				.format("Adresse [codePostal=%s, insee=%s, commune=%s, codeDepartement=%s, codeRegion=%s, codeDialogDepartement=%s, codeTGDepartement=%s]",
						getCodePostal(), getInsee(), commune, codeDepartement,
						codeRegion, codeDialogDepartement, codeTGDepartement);
	}


	/**
	 * @return the pk
	 */
	public AdressePK getPk() {
		return pk;
	}

	/**
	 * @param pk the pk to set
	 */
	public void setPk(AdressePK pk) {
		this.pk = pk;
	}

	
	@Embeddable
	public static class AdressePK implements Serializable {
		private static final long serialVersionUID = 1L;

		@Column(name="codePostal", length=5)
		private String codePostal;

		@Column(name="insee", length=5)
		private String insee;

		
		/**
		 * 
		 */
		public AdressePK() {
			super();
		}

		/**
		 * 
		 */
		public AdressePK(String codePostal, String insee) {
			super();
			this.codePostal = codePostal;
			this.insee = insee;
		}

		/**
		 * @return the codePostal
		 */
		public String getCodePostal() {
			return codePostal;
		}

		/**
		 * @param codePostal the codePostal to set
		 */
		public void setCodePostal(String codePostal) {
			this.codePostal = codePostal;
		}

		/**
		 * @return the insee
		 */
		public String getInsee() {
			return insee;
		}

		/**
		 * @param insee the insee to set
		 */
		public void setInsee(String insee) {
			this.insee = insee;
		}

		/* (non-Javadoc)
		 * @see java.lang.Object#hashCode()
		 */
		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result
					+ ((codePostal == null) ? 0 : codePostal.hashCode());
			result = prime * result + ((insee == null) ? 0 : insee.hashCode());
			return result;
		}

		/* (non-Javadoc)
		 * @see java.lang.Object#equals(java.lang.Object)
		 */
		@Override
		public boolean equals(Object obj) {
			if (this == obj) {
				return true;
			}
			if (obj == null) {
				return false;
			}
			if (!(obj instanceof AdressePK)) {
				return false;
			}
			AdressePK other = (AdressePK) obj;
			if (codePostal == null) {
				if (other.codePostal != null) {
					return false;
				}
			} else if (!codePostal.equals(other.codePostal)) {
				return false;
			}
			if (insee == null) {
				if (other.insee != null) {
					return false;
				}
			} else if (!insee.equals(other.insee)) {
				return false;
			}
			return true;
		}
	}

}
